<?php
namespace nimo\domains\car_balance\controllers\crud;

use BaseController;
use View;
use Input;
use Request;
use Redirect;
use Validator;
use nimo\domains\car_balance\Model\CustomerBillType;

class CustomerBillTypeController extends BaseController
{
    /**
     * Declare the rules for the form validation
     *
     * @var array
     */
    protected $validationRules = array(
            "customer_id"                => "required|max:80",
            "bill_type"                  => "required",
            "date_from"                  => "",
            "date_to"                    => "",
            "commission"                 => "",
    );

    /**
    * Show a list of all the banks.
    *
    * @return View
    */
    public function getIndex()
    {
        // Grab all the banks
        $customerBillTypes = new CustomerBillType();


        // Paginate the banks
        $customerBillTypes = $customerBillTypes->paginate(50)
        ;

        // Show the page
        return View::make('nimo.domains.car_balance.CustomerBillType.index', compact('customerBillTypes'));
    }

    /**
     * CustomerBillType create.
     *
     * @return View
     */
    public function getCreate()
    {
        // Show the page
        return View::make('nimo.domains.car_balance.CustomerBillType.create')
        ;
    }

    /**
     * CustomerBillType create form processing.
     *
     * @return Redirect
     */
    public function postCreate()
    {
        // Create a new validator instance from our validation rules
        $validator = Validator::make(Input::all(), $this->validationRules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails())
        {
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        }


        // Create a new customerBillType
        $customerBillType = new CustomerBillType();

        // Update the customerBillType data
        $customerBillType->customer_id                = e(Input::get('customer_id'));
        $customerBillType->bill_type                  = e(Input::get('bill_type'));
        $customerBillType->date_from                  = e(Input::get('date_from'));
        $customerBillType->date_to                    = e(Input::get('date_to'));
        $customerBillType->commission                 = e(Input::get('commission'));
        $customerBillType->setCreatedAt(date('Y-m-d'));

        // Was the customerBillType created?
        if($customerBillType->save())
        {
            // Redirect to the new customerBillType page
            return Redirect::route('car_balance.customerBillType.show', array('id' => $customerBillType->id))->with('success', 'CustomerBillType created successfully');
        }

        // Redirect to the customerBillType create page
        return Redirect::route('car_balance.customerBillType.create')->with('error', 'CustomerBillType was not created, please try again.');
    }

    /**
     * CustomerBillType update.
     *
     * @param  int  $id
     * @return View
     */
    public function getEdit($id = null)
    {
        // Check if the customerBillType exists
        if (is_null($customerBillType = CustomerBillType::find($id)))
        {
            // Redirect to the CustomerBillType management page
            return Redirect::route('car_balance.customerBillType.index')->with('error', "CustomerBillType [$id] does not exist.");
        }

        // Show the page
        return View::make('nimo.domains.car_balance.CustomerBillType.edit', compact('customerBillType'))
        ;
    }

    /**
     * CustomerBillType update form processing page.
     *
     * @param  int  $id
     * @return Redirect
     */
    public function postEdit($id = null)
    {
        // Check if the customerBillType exists
        if (is_null($customerBillType = CustomerBillType::find($id)))
        {
            // Redirect to the CustomerBillType management page
            return Redirect::route('car_balance.customerBillType.index')->with('error', "CustomerBillType [$id] does not exist.");
        }

        // Create a new validator instance from our validation rules
        $validator = Validator::make(Input::all(), $this->validationRules);

        // If validation fails, we'll exit the operation now.
        if ($validator->fails())
        {
            // Ooops.. something went wrong
            return Redirect::back()->withInput()->withErrors($validator);
        }

        // Update the customerBillType data
        $customerBillType->customer_id                = e(Input::get('customer_id'));
        $customerBillType->bill_type                  = e(Input::get('bill_type'));
        $customerBillType->date_from                  = e(Input::get('date_from'));
        $customerBillType->date_to                    = e(Input::get('date_to'));
        $customerBillType->commission                 = e(Input::get('commission'));
        $customerBillType->setUpdatedAt(date('Y-m-d'));

        // Was the customerBillType updated?
        if($customerBillType->save())
        {
            // Redirect to the CustomerBillType page
            return Redirect::route('car_balance.customerBillType.show', array('id' => $id))->with('success', 'CustomerBillType updated successfully.');
        }

        // Redirect to the CustomerBillType management page
        return Redirect::route('car_balance.customerBillType.edit', array('id' => $id))->with('error', 'CustomerBillType was not updated, please try again');
    }

    /**
     * CustomerBillType show.
     *
     * @param  int  $id
     * @return View
     */
    public function getShow($id = null)
    {
        // Check if the customerBillType exists
        if (is_null($customerBillType = CustomerBillType::find($id)))
        {
            // Redirect to the CustomerBillType management page
            return Redirect::route('car_balance.customerBillType.index')->with('error', "CustomerBillType [$id] does not exist.");
        }

        // Show the page
        return View::make('nimo.domains.car_balance.CustomerBillType.show', compact('customerBillType'));
    }

    /**
     * Delete the given customerBillType.
     *
     * @param  int  $id
     * @return Redirect
     */
    public function getDelete($id = null)
    {
        // Check if the customerBillType exists
        if (is_null($customerBillType = CustomerBillType::find($id)))
        {
            // Redirect to the CustomerBillType management page
            return Redirect::route('car_balance.customerBillType.index')->with('error', 'There was an issue deleting the CustomerBillType. Please try again.');
        }

        
        // Delete the customerBillType
        $customerBillType->delete();

        // Redirect to the CustomerBillType list page
        return Redirect::route("car_balance.customerBillType.index")->with('success', 'CustomerBillType was successfully deleted.');
    }
}